﻿using Lanbt.Core.Entity.Dto;
using Lanbt.Core.Entity.Dto.lanbt_system_config;
using Lanbt.Core.Entity.Dto.lanbt_user;
using Lanbt.Core.Service.SqlService.IService;
using Lanbt.Core.Utility;
using Lanbt.Core.Utility.ExceptionExtent;
using Lanbt.Core.Utility.Extent;
using Lanbt.Core.Web.Controllers.Base;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;

namespace Lanbt.Core.Web.Controllers.Action
{
    /// <summary>
    /// 首页
    /// </summary>
    public class HomeAction : ActionController
    {

        private readonly IUserService _userService;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IConfigService _configService;

        public HomeAction(IUserService userService, IHttpClientFactory httpClientFactory, IConfigService configService)
        {
            _userService = userService;
            _httpClientFactory = httpClientFactory;
            _configService = configService;
        }

        /// <summary>
        /// 检查Token是否健康
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.home.token.healthy")]
        public ResultOutput TokenHealthy()
        {
            return new ResultOutput();
        }

        /// <summary>
        /// 测试鉴权
        /// </summary>
        /// <returns></returns>
        [HttpPost("/lanbt.home.test")]
        public ResultOutput test()
        {
            return new ResultOutput().ResultInput("测试鉴权成功");
        }

        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.home.current.user.get")]
        public async Task<ResultOutput<GetCurrentUserOutput>> GetCurrentUser()
        {
            return await _userService.GetUserById(CurrentUser.Id);
        }

        /// <summary>
        /// 退出清除缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet("/lanbt.home.sign.out")]
        public ResultOutput LogOut()
        {
            Cache.CacheFactory.Cache.RemoveCache(String.Format(CacheKeys.login_userid, CurrentUser.Id));
            return new ResultOutput();
        }

        /// <summary>
        /// 解锁屏幕
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        [HttpGet("/lanbt.home.unlock")]
        public async Task<ResultOutput> Unlock([Required(ErrorMessage ="请输入密码")]string pass)
        {
            var result = new ResultOutput();
            var setting = await _configService.GetUserConfig(Entity.Enums.UserConfigKey.SystemSetting);
            if(setting.Success && setting.Result.IsNotNullOrEmpty())
            {
                UserSetting systemSetting = JsonConvert.DeserializeObject<UserSetting>(setting.Result);
                if(systemSetting == null) 
                {
                    throw new LanbtException(ExceptionModel.ENTITY_EXISTEN, "用户配置未设置");
                }
                if(systemSetting.screen_password != pass)
                {
                    return result.AddError("密码错误");
                }
                return result.ResultInput("正确");
            }
            return result.AddError("系统配置获取失败");
        }
    }
}
